<?php
/**
 * 个人中心-杂
 * Created by PhpStorm.
 * User: Earl <guo@seosite.cn>
 * Date: 2017/9/27
 * Time: 上午10:14
 * Updatetime:
 * updater：
 */
namespace Wap\Controller;

use Faker\Factory;

class CenterController extends BaseController{

    /**
     * @desc 消息类型：1 视频，2 音频，3 报告，4 图片，5 快讯，6 数据，7 会议，8 专栏，9 问答，10 问卷
     * @param $k
     */
    private function messageTypeText($k)
    {
        $list[1] = '视频';
        $list[2] = '音频';
        $list[3] = '报告';
        $list[4] = '图片';
        $list[5] = '快讯';
        $list[6] = '数据';
        $list[7] = '会议';
        $list[8] = '专栏';
        $list[9] = '问答';
        $list[10] = '问卷';
        $list[11] = '充值';
        $list[12] = '提现';
        return $list[$k];
    }

    /**
     * @desc 消息列表
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     * @param $param
     * @return array
     */
    public function msgList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $uid = $this->userInfo->id;
        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model = M('Message');
        $map['uid'] = $uid;

        list($listRet, $page) = _listPage($model, $map, $p, 'is_read asc , create_time desc', 'id,title,content,is_read,type,create_time', $listRows);
        if (!empty($listRet)){
            foreach ($listRet as &$v){
                $v['typeText'] = $this->messageTypeText($v['type']);
                $v['create_time'] = date('Y/m/d',strtotime($v['create_time']));
            }
        }
        return showList($listRet, $page);
    }

    /**
     * @desc 未读列表
     * @param $param 是否已读,0 未读，1 已读
     * @return array
     */
    public function unReadMsgList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $uid = $this->userInfo->id;
        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $model = M('Message');
        $map['uid'] = $uid;
        $map['is_read'] = 0;

        list($listRet, $page) = _listPage($model, $map, $p, 'is_read asc , create_time desc', 'id,title,content,is_read,type,create_time', $listRows);
        if (!empty($listRet)){
            foreach ($listRet as &$v){
                $v['typeText'] = $this->messageTypeText($v['type']);
                $v['create_time'] = date('Y/m/d',strtotime($v['create_time']));
            }
        }
        return showList($listRet, $page);
    }

    /**
     * @desc 全部设为已读
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     * @return array
     */
    public function setMsgRead()
    {
        $check = CheckFactoryController::isLogin();
        $check->before(null);

        unset($check);

        $uid = $this->userInfo->id;
        if (empty($uid)) return showErrorMsg(L('_TOKEN_USELESS_'),100004);
        M('message')->where(array('user_id'=>$uid,'is_read'=>0))->setField('read_time',NOW_TIME);
        M('message')->where(array('user_id'=>$uid,'is_read'=>0))->setField('is_read',1);
        return showErrorMsg(L('_OPERATION_SUCCESS_'),0,2,new \stdClass());
    }

    /**
     * @desc 消息详情
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     * @param $param
     * @return array
     */
    public function msgDetail($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $id = $param['id'];
        $msgDetail = M('message')->field('id,title,content,create_time')->find($id);
        if (empty($msgDetail)) return showErrorMsg('消息不存在',100002);
        if (empty($msgDetail['is_read'])){
            M('message')->where(array('id'=>$msgDetail['id']))->setField('read_time',NOW_TIME);
            M('message')->where(array('id'=>$msgDetail['id']))->setField('is_read',1);
        }
        $msgDetail['create_time'] = date('Y-m-d',$msgDetail['create_time']);
        return showErrorMsg(L('_OPERATION_SUCCESS_'),0,2,$msgDetail);
    }

    /**
     * @desc 删除消息
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     * @param $param
     * @return array
     */
    public function delMsg($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $id = $param['id'];
        $operation = M('message')->where(array('id'=>array('in',$id)))->delete();
        if ($operation) return showErrorMsg(L('_OPERATION_SUCCESS_'));
        return showErrorMsg(L('_OPERATION_FAIL_'),100002);
    }

    /**
     * @desc 清空消息
     * @return array
     */
    public function clearMsg()
    {
        $check = CheckFactoryController::isLogin();
        $check->before(null);

        unset($check);

        $operation = M('message')->where(array('uid'=>$this->userInfo->id))->delete();
        if ($operation) return showErrorMsg(L('_OPERATION_SUCCESS_'));
        return showErrorMsg(L('_OPERATION_FAIL_'),100002);
    }


    private function moneySceneText($k)
    {
        $list[1] = '视频';
        $list[2] = '音频';
        $list[3] = '报告';
        $list[4] = '图片';
        $list[5] = '快讯';
        $list[6] = '数据';
        $list[7] = '会议';
        $list[8] = '专栏';
        $list[9] = '问答';
        $list[10] = '问卷';
        return $list[$k];
    }

    /**
     * @desc 我的收入、支出
     * @param $param
     * @return array
     */
    public function inCome($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('start_time,end_time,scene');

        unset($check);

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $start_time = strtotime($param['start_time']);
        $end_time = strtotime($param['end_time']);
        $scene = $param['scene'];
        if (!empty($scene)){
            if (count(explode(',',$scene))==1){
                $map['scene'] = $scene;
            }else{
                $map['scene'] = array('in',$scene);
            }
        }

        $map['type'] = $param['type'];

        $model = M('money_record');
        $map['create_time'] = array('between',array($start_time,$end_time));

        if ($param['type']==1){//收入
            $map['puid'] = $this->userInfo->id;
        }else{
            $map['uid'] = $this->userInfo->id;
        }

        $RetData = $model->field("sum(amount) amount,scene")->where($map)->group('scene')->select();
        $amount = $model->where($map)->sum('amount');

        list($list,$page) = _listPage($model,$map,$p,'create_time desc','id,puid,uid,amount,beizhu,scene,create_time',$listRows);


        if (!empty($RetData)){
            foreach ($RetData as &$v){
                $v['sceneText'] = $this->moneySceneText($v['scene']);
                $v['pencent'] = number_format($v['amount']/$amount,2);
            }
        }

        if ($param['type']==1){
            if (!empty($list)){
                foreach ($list as $k1=>$v1){
                    $list[$k1]['sceneText'] = $this->moneySceneText($v1['scene']);
                    $info = M('User')->where(['id'=>$v1['uid']])->find();
                    $list[$k1]['p_u_no'] = $info['u_no'];
                    $list[$k1]['p_name'] = $info['nickname'];
                    $list[$k1]['p_phone'] = $info['phone'];
                    $list[$k1]['create_time'] = date('Y/m/d',strtotime($v1['create_time']));

                }
            }
        }else{
            if (!empty($list)){
                foreach ($list as $k1=>$v1){
                    $list[$k1]['sceneText'] = $this->moneySceneText($v1['scene']);
                    $info = M('User')->where(['id'=>$v1['puid']])->find();
                    $list[$k1]['p_u_no'] = $info['u_no'];
                    $list[$k1]['p_name'] = $info['company'];
                    $list[$k1]['create_time'] = date('Y/m/d',strtotime($v1['create_time']));
                }
            }
        }


        $Ret = array(
            'chat_list'  =>  $RetData,
            'sum'        =>  empty($amount)?'0.00':$amount,
            'data_list'  =>  $list
        );

        return showErrorMsg(L('_OPERATION_SUCCESS_'),0,2,$Ret);
    }


    /**
     * @desc 余额
     * @return array
     */
    public function balance()
    {
        $check = CheckFactoryController::isLogin();
        $check->before(null);

        unset($check);

        $balance = M('User')->where(['id'=>$this->userInfo->id])->getField('balance');

        $data = array(
            'balance'   =>  $balance,
            'explain'   =>  C('balance_explain')
        );

        return showErrorMsg(L('_OPERATION_SUCCESS_'),0,2,$data);
    }

    /**
     * @desc 添加银行卡
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     * @param $param
     * @return array
     */
    public function addBank($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('bank_name,username,bank_no');

        unset($check);

        $add_data =array(
            'uid'   =>  $this->userInfo->id,
            'bank' =>  $param['bank_name'],
            'realname'  =>  $param['username'],
            'bank_id'   =>$param['bank_no'],
            'create_time'=>NOW_TIME
        );

        $insertID = M('bank')->add($add_data);
        if ($insertID) return showErrorMsg(L('_OPERATION_SUCCESS_'),0,2,new \stdClass());

        return showErrorMsg(L('_OPERATION_FAIL_'),100002);
    }

    /**
     * @desc 删除银行卡
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     * @param $param
     * @return array
     */
    public function removeBank($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $id = $param['id'];

        $removeRet = M('bank')->delete($id);

        if ($removeRet) return showErrorMsg(L('_OPERATION_SUCCESS_'),0,2,new \stdClass());
        return showErrorMsg('删除失败',100002);
    }

    /**
     * @desc 提现
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     * @param $param
     * @return array
     */
    public function draw($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('amount');

        unset($check);

        $add_data = array(
                'uid'   =>  $this->userInfo->id,
                'type'  =>  2,
                'amount'  =>  $param['amount'],
                'code'  =>  '100001',
                'status'  =>  '1',
                'beizhu'  =>  '用户提现',
                'scene'  =>  '11',
                'create_time'  =>  time()
        );
        $operationID = M('money_record')->add($add_data);
        if($operationID) return showErrorMsg(L('_OPERATION_SUCCESS_'));
        return showErrorMsg(L('_OPERATION_FAIL_'),100002);
    }

    /**
     * @desc
     * @author Earl <guo@seosite.cn> .
     * @updateTime
     * @param $param
     * @return array
     */
    public function submitComplain($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('content');

        unset($check);

        $uid = $this->userInfo->id;
        $content = $param['content'];
        $pic1   =   empty($param['pic1'])?'':$param['pic1'];
        $pic2   =   empty($param['pic2'])?'':$param['pic2'];
        $pic3   =   empty($param['pic3'])?'':$param['pic3'];

        $add_data = array(
            'user_id'   =>  $uid,
            'content'   =>  $content,
            'create_time'=> NOW_TIME,
            'pic1'      =>  $pic1,
            'pic2'      =>  $pic2,
            'pic3'      =>  $pic3,
            'status'    =>  0
        );
        $insertID = M('complain')->add($add_data);
        if (!$insertID) return showErrorMsg('提交失败，请检查网络!',100002);
        return showErrorMsg(L('_OPERATION_SUCCESS_'),0,2,new \stdClass());
    }

    /**
     * @desc 申请成为博主
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function applyBo($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $model = M('User');

        $userinfo = $model->find($this->userInfo->id);

        if (empty($userinfo)) return showErrorMsg('用户不存在',100002);

        if (empty($userinfo['nickname']) || empty($userinfo['headsmall']) || empty($userinfo['phone']) || empty($userinfo['career']) || empty($userinfo['email_address']) || empty($userinfo['company']) || empty($userinfo['u_intro'])) return showErrorMsg('资料完善后才可以申请博主',100002);


        $model->where(['id'=>$this->userInfo->id])->setField('bo_status',3);
        $model->where(['id'=>$this->userInfo->id])->setField('role',2);
        $model->where(['id'=>$this->userInfo->id])->setField('update_time',time());

        return showErrorMsg(L('_OPERATION_SUCCESS_'));
    }


    /**
     * @desc 站点统计
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function siteData($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('act_start_time,act_end_time,access_start_time,access_end_time,content_start_time,content_end_time,reg_start_time,reg_end_time');

        unset($check);


        $act_start_time = $param['act_start_time'];
        $act_end_time =$param['act_end_time'];

        $days =  round( (strtotime($act_end_time)-strtotime($act_start_time))/3600/24 );
        $temp = [];
        for ($i=0;$i<=$days;$i++){
            $temp[] = date('Y/m/d',strtotime($act_start_time)+($i*86400));
        }

        $act_map['uid'] = $this->userInfo->id;
        $act_map['create_time'] = array('between',array(strtotime($param['act_start_time']),strtotime($param['act_end_time'])));

        $field = "from_unixtime(create_time,'%Y/%m/%d') x,count(*) y";
        $praise = M('comment_parise')->field($field)->where($act_map)->group('x')->select();

        $comment = M('comment a')->field($field)->where($act_map)->group('x')->select();

        $connect = M('connect')->field($field)->where($act_map)->group('x')->select();

        $recommend = M('recommend')->field($field)->where($act_map)->group('x')->select();
        $collect = M('collect')->field($field)->where($act_map)->group('x')->select();

        $tempArr = [];

        foreach ($temp as $k=>$v){

            $tempArr['praise']['color'] = '#699bd0';
            $tempArr['comment']['color'] = '#df8244';
            $tempArr['recommend']['color'] = '#a5a5a5';
            $tempArr['connect']['color'] = '#f6c142';
            $tempArr['collect']['color'] = '#4d73be';

            if (empty($praise)){

                $tempArr['praise']['list'][$k]['x'] = $v;
                $tempArr['praise']['list'][$k]['y'] = '';

            }else{
                foreach ($praise as $k1=>$v1){
                    $tempArr['praise']['list'][$k]['x'] = $v;
                    if ($v==$v1['x']){
                        $tempArr['praise']['list'][$k]['y'] = $v1['y'];
                        break;
                    }else{
                        $tempArr['praise']['list'][$k]['y'] = '';
                    }
                }

            }

            if (empty($comment)){
                $tempArr['comment']['list'][$k]['x'] = $v;
                $tempArr['comment']['list'][$k]['y'] = '';
            }else{
                foreach ($comment as $k1=>$v1){
                    $tempArr['comment']['list'][$k]['x'] = $v;
                    if ($v==$v1['x']){
                        $tempArr['comment']['list'][$k]['y'] = $v1['y'];
                        break;
                    }else{
                        $tempArr['comment']['list'][$k]['y'] = '';
                    }
                }
            }

            if (empty($recommend)){
                $tempArr['recommend']['list'][$k]['x'] = $v;
                $tempArr['recommend']['list'][$k]['y'] = '';

            }else{
                foreach ($recommend as $k1=>$v1){
                    $tempArr['recommend']['list'][$k]['x'] = $v;
                    if ($v==$v1['x']){
                        $tempArr['recommend']['list'][$k]['y'] = $v1['y'];
                        break;
                    }else{
                        $tempArr['recommend']['list'][$k]['y'] = '';
                    }
                }
            }

            if (empty($connect)){
                $tempArr['connect']['list'][$k]['x'] = $v;
                $tempArr['connect']['list'][$k]['y'] = '';

            }else{
                foreach ($connect as $k1=>$v1){
                    $tempArr['connect']['list'][$k]['x'] = $v;
                    if ($v==$v1['x']){
                        $tempArr['connect']['list'][$k]['y'] = $v1['y'];
                        break;
                    }else{
                        $tempArr['connect']['list'][$k]['y'] = '';
                    }
                }
            }



            if (empty($collect)){

                $tempArr['collect']['list'][$k]['x'] = $v;
                $tempArr['collect']['list'][$k]['y'] = '';


            }else{
                foreach ($collect as $k1=>$v1){
                    $tempArr['collect']['list'][$k]['x'] = $v;
                    if ($v==$v1['x']){
                        $tempArr['collect']['list'][$k]['y'] = $v1['y'];
                        break;
                    }else{
                        $tempArr['collect']['list'][$k]['y'] = '';
                    }
                }
            }


        }

        //用户访问
        $access_map['create_time'] = array('between',array(strtotime($param['act_start_time']),strtotime($param['act_end_time'])));
        $access_map['uid'] = $this->userInfo->id;

        $views_map['create_time'] = array('between',array(strtotime($param['act_start_time']),strtotime($param['act_end_time'])));
        $views_map['bo_id'] = $this->userInfo->id;

        $views_field = "from_unixtime(create_time,'%Y/%m/%d') x,count(*) y";
        $views = M('uuid_bo')->field($views_field)->where($views_map)->group('x')->select();

        $access_start_time = $param['access_start_time'];
        $access_end_time = $param['access_end_time'];

        $days =  round( (strtotime($access_end_time)-strtotime($access_start_time))/3600/24 );
        $temp3 = [];
        for ($i=0;$i<=$days;$i++){
            $temp3[] = date('Y/m/d',strtotime($access_start_time)+($i*86400));
        }

        $tempArr3 = [];

        foreach ($temp3 as $k=>$v){

            $tempArr3['views']['color'] = '#699bd0';
            $tempArr3['all_time']['color'] = '#df8244';
            $tempArr3['ave_time']['color'] = '#a5a5a5';

            if (empty($views)){

                $tempArr3['views']['list'][$k]['x'] = $v;
                $tempArr3['views']['list'][$k]['y'] = '';

            }else{
                foreach ($views as $k1=>$v1){
                    $tempArr3['views'][$k]['x'] = $v;
                    if ($v==$v1['x']){
                        $tempArr3['views']['list'][$k]['y'] = $v1['y'];
                        break;
                    }else{
                        $tempArr3['views']['list'][$k]['y'] = '';
                    }
                }
            }


            $tempArr3['all_time']['list'][$k]['x'] = $v;
            $tempArr3['all_time']['list'][$k]['y'] = '';

            $tempArr3['ave_time']['list'][$k]['x'] = $v;
            $tempArr3['ave_time']['list'][$k]['y'] = '';

        }


        //内容发布
        $content_map['create_time'] = array('between',array(strtotime($param['content_start_time']),strtotime($param['content_end_time'])));
        $content_map['uid'] = $this->userInfo->id;

        $content_field = "from_unixtime(create_time,'%Y/%m/%d') x,count(*) y";
        $video = M('video')->field($content_field)->where($content_map)->group('x')->select();
        $audio = M('audio')->field($content_field)->where($content_map)->group('x')->select();
        $report = M('report')->field($content_field)->where($content_map)->group('x')->select();
        $picture = M('picture')->field($content_field)->where($content_map)->group('x')->select();
        $meeting = M('meeting')->field($content_field)->where($content_map)->group('x')->select();
        $question = M('question')->field($content_field)->where($content_map)->group('x')->select();
        $qa = M('qa')->field($content_field)->where($content_map)->group('x')->select();

        $content_start_time = $param['content_start_time'];
        $content_end_time = $param['content_end_time'];

        $days =  round( (strtotime($content_end_time)-strtotime($content_start_time))/3600/24 );
        $temp2 = [];
        for ($i=0;$i<=$days;$i++){
            $temp2[] = date('Y/m/d',strtotime($content_start_time)+($i*86400));
        }

        $tempArr2 = [];

        foreach ($temp2 as $k=>$v){

            $tempArr2['video']['color'] = '#699bd0';
            $tempArr2['audio']['color'] = '#df8244';
            $tempArr2['report']['color'] = '#a5a5a5';
            $tempArr2['picture']['color'] = '#f6c142';
            $tempArr2['meeting']['color'] = '#4d73be';
            $tempArr2['question']['color'] = '#7eaa55';
            $tempArr2['qa']['color'] = '#335e8d';


            if (empty($video)){

                $tempArr2['video']['list'][$k]['x'] = $v;
                $tempArr2['video']['list'][$k]['y'] = '';

            }else{
                foreach ($video as $k1=>$v1){
                    $tempArr2['video'][$k]['x'] = $v;
                    if ($v==$v1['x']){
                        $tempArr2['video']['list'][$k]['y'] = $v1['y'];
                        break;
                    }else{
                        $tempArr2['video']['list'][$k]['y'] = '';
                    }
                }

            }

            if (empty($audio)){
                $tempArr2['audio']['list'][$k]['x'] = $v;
                $tempArr2['audio']['list'][$k]['y'] = '';
            }else{
                foreach ($audio as $k1=>$v1){
                    $tempArr2['audio']['list'][$k]['x'] = $v;
                    if ($v==$v1['x']){
                        $tempArr2['audio']['list'][$k]['y'] = $v1['y'];
                        break;
                    }else{
                        $tempArr2['audio']['list'][$k]['y'] = '';
                    }
                }
            }

            if (empty($report)){
                $tempArr2['report']['list'][$k]['x'] = $v;
                $tempArr2['report']['list'][$k]['y'] = '';

            }else{
                foreach ($report as $k1=>$v1){
                    $tempArr2['report']['list'][$k]['x'] = $v;
                    if ($v==$v1['x']){
                        $tempArr2['report']['list'][$k]['y'] = $v1['y'];
                        break;
                    }else{
                        $tempArr2['report']['list'][$k]['y'] = '';
                    }
                }
            }

            if (empty($picture)){
                $tempArr2['picture']['list'][$k]['x'] = $v;
                $tempArr2['picture']['list'][$k]['y'] = '';

            }else{
                foreach ($picture as $k1=>$v1){
                    $tempArr2['picture']['list'][$k]['x'] = $v;
                    if ($v==$v1['x']){
                        $tempArr2['picture']['list'][$k]['y'] = $v1['y'];
                        break;
                    }else{
                        $tempArr2['picture']['list'][$k]['y'] = '';
                    }
                }
            }

            if (empty($meeting)){
                $tempArr2['meeting']['list'][$k]['x'] = $v;
                $tempArr2['meeting']['list'][$k]['y'] = '';
            }else{
                foreach ($meeting as $k1=>$v1){
                    $tempArr2['meeting']['list'][$k]['x'] = $v;
                    if ($v==$v1['x']){
                        $tempArr2['meeting']['list'][$k]['y'] = $v1['y'];
                        break;
                    }else{
                        $tempArr2['meeting']['list'][$k]['y'] = '';
                    }
                }
            }

            if (empty($question)){
                $tempArr2['question']['list'][$k]['x'] = $v;
                $tempArr2['question']['list'][$k]['y'] = '';
            }else{
                foreach ($question as $k1=>$v1){
                    $tempArr2['question']['list'][$k]['x'] = $v;
                    if ($v==$v1['x']){
                        $tempArr2['question']['list'][$k]['y'] = $v1['y'];
                        break;
                    }else{
                        $tempArr2['question']['list'][$k]['y'] = '';
                    }
                }
            }

            if (empty($qa)){
                $tempArr2['qa']['list'][$k]['x'] = $v;
                $tempArr2['qa']['list'][$k]['y'] = '';
            }else{
                foreach ($qa as $k1=>$v1){
                    $tempArr2['qa']['list'][$k]['x'] = $v;
                    if ($v==$v1['x']){
                        $tempArr2['qa']['list'][$k]['y'] = $v1['y'];
                        break;
                    }else{
                        $tempArr2['qa']['list'][$k]['y'] = '';
                    }
                }
            }
        }


        //注册、关注数，累计注册、关注数
        $reg_map['reg_time'] = array('between',array(strtotime($param['reg_start_time']),strtotime($param['reg_end_time'])));
        $reg_map['t_id'] = $this->userInfo->id;

        $connect_map['create_time'] = array('between',array(strtotime($param['reg_start_time']),strtotime($param['reg_end_time'])));
        $connect_map['puid'] = $this->userInfo->id;

        $regs_field = "from_unixtime(reg_time,'%Y/%m/%d') x,count(*) y";
        $regs = M('uuid_bo')->field($regs_field)->where($reg_map)->group('x')->select();


        $connect_field = "from_unixtime(create_time,'%Y/%m/%d') x,count(*) y";
        $connects = M('user_follow')->field($connect_field)->where($connect_map)->group('x')->select();


        $reg_start_time = $param['reg_start_time'];
        $reg_end_time = $param['reg_end_time'];

        $days =  round( (strtotime($reg_end_time)-strtotime($reg_start_time))/3600/24 );
        $temp4 = [];
        for ($i=0;$i<=$days;$i++){
            $temp4[] = date('Y/m/d',strtotime($reg_start_time)+($i*86400));
        }


        $userModel = M('user');
        $user_map['t_id'] = $this->userInfo->id;

        $connectModel = M('user_follow');
        $connect_map['puid'] = $this->userInfo->id;

        $tempArr4 = [];

        foreach ($temp4 as $k=>$v){

            $tempArr4['reg']['color'] = '#699bd0';
            $tempArr4['connect']['color'] = '#df8244';
            $tempArr4['reg_all']['color'] = '';
            $tempArr4['connect_all']['color'] = '';

            if (empty($regs)){

                $tempArr4['reg']['list'][$k]['x'] = $v;
                $tempArr4['reg']['list'][$k]['y'] = '';

            }else{
                foreach ($views as $k1=>$v1){
                    $tempArr4['reg'][$k]['x'] = $v;
                    if ($v==$v1['x']){
                        $tempArr4['reg']['list'][$k]['y'] = $v1['y'];
                        break;
                    }else{
                        $tempArr4['reg']['list'][$k]['y'] = '';
                    }
                }
            }


            if (empty($connects)){

                $tempArr4['connect']['list'][$k]['x'] = $v;
                $tempArr4['connect']['list'][$k]['y'] = '';

            }else{
                foreach ($connects as $k1=>$v1){
                    $tempArr4['connect'][$k]['x'] = $v;
                    if ($v==$v1['x']){
                        $tempArr4['connect']['list'][$k]['y'] = $v1['y'];
                        break;
                    }else{
                        $tempArr4['connect']['list'][$k]['y'] = '';
                    }
                }
            }


            $start_time = strtotime($v);
            $user_map['reg_time'] = array('lt',$start_time);

            $all_reg = $userModel->where($user_map)->sum();
            $tempArr4['reg_all']['list'][$k]['x'] = $v;
            $tempArr4['reg_all']['list'][$k]['y'] = empty($all_reg)?"":$all_reg;



            $connect_map['create_time'] = array('lt',$start_time);

            $all_connect = $connectModel->where($connect_map)->sum();
            $tempArr4['connect_all']['list'][$k]['x'] = $v;
            $tempArr4['connect_all']['list'][$k]['y'] = empty($all_connect)?"":$all_connect;

        }

        $ret_data = array(
            'act'   =>  $tempArr,
            'access'   =>  $tempArr3,
            'content'   =>  $tempArr2,
            'reg'   =>  $tempArr4,
        );

        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$ret_data);
    }


    /**
     * @desc 收费设置
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function feeSite($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $model = M('user_auth');

        $map['uid'] = $this->userInfo->id;

        $info = $model->where($map)->find();

        $fee = !empty($info['fee'])?$info['fee']:'';

        return showErrorMsg(L('_OPERATION_SUCCESS_'),0,2,array('fee'=>$fee));
    }

    /**
     * @desc 保存收费设置
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function updateFeeSite($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $model = M('user_auth');

        $model->where(['uid'=>$this->userInfo->id])->delete();

        $insert_data = array(
            'title'     =>  'qa',
            'fee'       =>  $param['fee'],
            'pwd'       =>  '',
            'auth'      =>  '',
            'uid'       =>  $this->userInfo->id
        );
        $operation = $model->add($insert_data);
        if($operation) return showErrorMsg(L("_OPERATION_SUCCESS_"));
        return showErrorMsg(L("_OPERATION_FAIL_"),100002);

//        $insert_data_video = array(
//            'title' =>  'video',
//            'fee'   =>  $param['video_fee'],
//            'pwd'   =>  $param['video_pwd'],
//            'auth'   =>  $param['video_auth'],
//            'uid'   =>  $this->userInfo->id,
//        );
//
//        $insert_data_audio = array(
//            'title' =>  'audio',
//            'fee'   =>  $param['audio_fee'],
//            'pwd'   =>  $param['audio_pwd'],
//            'auth'   =>  $param['audio_auth'],
//            'uid'   =>  $this->userInfo->id,
//        );
//
//        $insert_data_report = array(
//            'title' =>  'report',
//            'fee'   =>  $param['report_fee'],
//            'pwd'   =>  $param['report_pwd'],
//            'auth'   =>  $param['report_auth'],
//            'uid'   =>  $this->userInfo->id,
//        );
//
//        $insert_data_picture = array(
//            'title' =>  'picture',
//            'fee'   =>  $param['picture_fee'],
//            'pwd'   =>  $param['picture_pwd'],
//            'auth'   =>  $param['picture_auth'],
//            'uid'   =>  $this->userInfo->id,
//        );
//
//        $insert_data_qa = array(
//            'title' =>  'qa',
//            'fee'   =>  $param['qa_fee'],
//            'pwd'   =>  $param['qa_pwd'],
//            'auth'   =>  $param['qa_auth'],
//            'uid'   =>  $this->userInfo->id,
//        );
//        $insert_data_question = array(
//            'title' =>  'question',
//            'fee'   =>  $param['question_fee'],
//            'pwd'   =>  $param['question_pwd'],
//            'auth'   =>  $param['question_auth'],
//            'uid'   =>  $this->userInfo->id,
//        );
//        $insert_data_meeting = array(
//            'title' =>  'meeting',
//            'fee'   =>  $param['meeting_fee'],
//            'pwd'   =>  $param['meeting_pwd'],
//            'auth'   =>  $param['meeting_auth'],
//            'uid'   =>  $this->userInfo->id,
//        );
//
//
//        $model->add($insert_data_video);
//        $model->add($insert_data_audio);
//        $model->add($insert_data_picture);
//        $model->add($insert_data_report);
//        $model->add($insert_data_meeting);
//        $model->add($insert_data_qa);
//        $model->add($insert_data_question);

        return showErrorMsg(L("_OPERATION_SUCCESS_"));
    }


    /**
     * @desc
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function bannerList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('type');

        unset($check);


        $model = M('banner');

        $map['uid'] = $this->userInfo->id;
        $map['banner_type'] = $param['type'];

        list($list,$page) = _listPage($model,$map,1,null,'id,title,headsmall,bannerurl,banner_type',100);

        return showList($list,$page);
    }

    /**
     * @desc 发布banner
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function addBanner($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('type,headsmall,bannerurl');

        unset($check);

        $model = M('banner');

        if(!empty($param['id'])){
            $update_data = array(
                'headsmall' =>  $param['headsmall'],
                'bannerurl' =>  $param['bannerurl']
            );
            $operation = $model->where(['id'=>$param['id']])->save($update_data);
        }else{
            $insert_data = array(
                'banner_type'   =>  $param['type'],
                'headsmall'     =>  $param['headsmall'],
                'bannerurl'     =>  $param['bannerurl'],
                'uid'           =>  $this->userInfo->id,
            );

            $operation = $model->add($insert_data);
        }

        if ($operation) return showErrorMsg(L('_OPERATION_SUCCESS_'));
        return showErrorMsg(L("OPERATION_FAIL_"),0);
    }

    /**
     * @desc 删除banner
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function removeBanner($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id');

        unset($check);

        $model = M('banner');

        $operation = $model->where(['id'=>$param['id']])->delete();
        if ($operation) return showErrorMsg(L('_OPERATION_SUCCESS_'));
        return showErrorMsg(L("OPERATION_FAIL_"),0);
    }

    /**
     * @desc 公共nav list+说明
     * @author Egor<http://gitee.com/seosite>
     * @return array
     */
    public function navList($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);
        unset($check);
        $nav_fee_list = C('nav_fee');

        foreach ($nav_fee_list as $k=>$v){

            $map['uid'] = $this->userInfo->id;
            $map['deadline'] = array('gt',time());
            $map['modul_name']= $v['name'];
            $map['status']= 1;
            $is_ixist = M('user_nav')->where($map)->find();


            if ($is_ixist) unset($nav_fee_list[$k]);
        }
        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,array_merge($nav_fee_list));
    }

    /**
     * @desc 会员费用设置
     * @author guokangle
     * @return array
     */
    public function vip_fee($param){
        $check = CheckFactoryController::isLogin();
        $check->before(null);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('id,one_m,two_m,three_m,six_m,twelve_m,months,fee,show');
        unset($check);

        //判断是否本人操作
        if ($param['id'] != $this->userInfo->id) return showErrorMsg("没有该操作的权限！", 100002);

        $model=M('vip_fee');
        $uid=$param['id'];
        if ($param['show']==1){
            $ret=M('vip_fee')->where("uid = $uid")->find();
            return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$ret);
        }
        if ($param['show']==0) {
            $gg = $model->where("uid = $uid")->find();
            if ($gg) {
                $map_save = [
                    'one_m' => $param['one_m'],
                    'two_m' => $param['two_m'],
                    'three_m' => $param['three_m'],
                    'six_m' => $param['six_m'],
                    'twelve_m' => $param['twelve_m'],
                    'months' => $param['months'],
                    'fee' => $param['fee'],
                    'uid' => $uid
                ];
                $re = $model->where("uid = $uid")->save($map_save);
                if ($re) {
                    return showErrorMsg(L('_OPERATION_SUCCESS_'), 0, 2);
                } else {
                    return showErrorMsg(L("_OPERATION_FAIL_"), 100003);
                }
            } else {
                $map_save = [
                    'one_m' => $param['one_m'],
                    'two_m' => $param['two_m'],
                    'three_m' => $param['three_m'],
                    'six_m' => $param['six_m'],
                    'twelve_m' => $param['twelve_m'],
                    'months' => $param['months'],
                    'fee' => $param['fee'],
                    'uid' => $uid,
                    'create_time' => time()
                ];
                $re = $model->add($map_save);
                if ($re) {
                    return showErrorMsg(L('_OPERATION_SUCCESS_'), 0, 2);
                } else {
                    return showErrorMsg(L("_OPERATION_FAIL_"), 100003);
                }
            }
        }
    }

    /**
     * @desc 个人中心搜索
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function search($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before(null);

        $check = CheckFactoryController::isParamSet();
        $check->before($param);

        $check = CheckFactoryController::isChildParamSet();
        $check->before('order,type,keyword');

        unset($check);

        $p = empty($param['p']) ?: $param['p'];
        $listRows = empty($param['listRows']) ? 20 : $param['listRows'];

        $len_keyword = mb_strwidth($param['keyword']);
        if ($len_keyword < 4) return showErrorMsg('关键字中文个数需要大于等于2，英文字母个数大于等于4',100002);

        $esObj = new ElasticSearchController();

        $tbName = $param['type'];

        if (empty($tbName)){
            $search_data = [
                'title' =>  $param['keyword'],
                'uid'   =>  $this->userInfo->id
            ];
        }else{
            $search_data = [
                'title' =>  $param['keyword'],
                'type'  =>  $tbName,
                'uid'   =>  $this->userInfo->id
            ];
        }

        $from = ($p-1)*$listRows;

        $size = $listRows;

        $sortName = $param['order']==1?'create_time':'views';

        return $esObj->filterSearch($search_data,$from,$size,$sortName,'desc');
    }
}